# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
from collections import defaultdict
import math
def selectLoanProduct(userParameter):
    
    return candidateList,precisionList
def loadData(path):
    dtype={'label':str}
    df = pd.read_csv(path,header=0,sep=',',engine='c',dtype=dtype)
    X = df.values
    return X.astype(str) 
def logV(x):
    if abs(x)==0:
        return 0
    else:
        return abs(x)*math.log(abs(x))
def selectQuestion(userParameter):            
    X = loadData('loan.csv')  
    loanName = np.array([x.decode('utf-8') for x in X[:,0]])
    loanMinRare = np.array([float(x) for x in X[:,6]])
    
    loan_minRate = defaultdict(float)
    for loanN,loanMR in zip(loanName,loanMinRare):
        loan_minRate[loanN]=loanMR
        
    userParameter=defaultdict()
    candidateList,precisionList=selectLoanProduct(userParameter)   
    if len(candidateList)==0:
        return u""
    minValue=float(100.0)
    minLoan=[]
    for canLoan in candidateList:
        if loan_minRate[canLoan]<minValue:
            minValue=loan_minRate[canLoan]
            minLoan=[]
            minLoan.append(canLoan)
        elif loan_minRate[canLoan]==minValue:
            minLoan.append(canLoan)
    
    hit=True#true表明停止询问，否则继续询问
    for ml in minLoan:
        if ml not in precisionList:
            hit=False
    Y= loadData('loan_questions.csv') 
    loanCon = np.array([y.decode('utf-8') for y in Y[:,0]])
    loanConQuestion= np.array([y.decode('utf-8') for y in Y[:,1]]) 
    cond_problem = defaultdict(str)
    for loanC,loanCQ in zip(loanCon,loanConQuestion):
            cond_problem[loanC]=loanCQ
    score=0 
    loan_score= defaultdict(float) 
    diff=True
    str1=u"符合您要求的有"
    str2=u"如果您满足"
    if hit==True:
        for preli in precisionList:
            str1=str1+preli
        return str1
    else:
        if userParameter["loanAmount"]=="none":
            return cond_problem["loanAmount"]
        else:
            for loanc in loanCon:      
                if userParameter[loanc]=="none" and loanc!="loanAmount":
                    userParameter[loanc]="yes"
                    candlist1,precList1=selectLoanProduct(userParameter) 
                    userParameter[loanc]="no"
                    candlist2,precList2=selectLoanProduct(userParameter) 
                    if(len(list(set(candlist1).difference(set(precList1))))==0 and len(list(set(candlist2).difference(set(precList2))))==0):
                        for preli in precisionList:
                            str1=str1+preli
                            return str1+"您还可以回答这个问题"+cond_problem[loanc]
                        diff=False
                        break
                    score=logV(len(candlist1)-len(candidateList))+logV(len(precList1)-len(precisionList)) + logV(len(candlist2)-len(candidateList))+logV(len(precList2)-len(precisionList))
                    loan_score[loanc]=score
                    loan_score[loanc]=="none"
            if diff:
                return cond_problem[max(loan_score.items(), key=lambda x: x[1])[0]]
            
            